﻿using IntexReport.DataSource;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class FLTICCGLLG : DocumentBase
    {
        #region-------------Properties-----------------------
        int ThreadCount = 0;
        int finishedcount = 0;
        int totalrecord = 0;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        private SetData src
        {
            get
            {
                return (SetData)TryFindResource("dataSource");
            }
        }/*
        private string LO
        {
            get
            {
                return ((TextBox)FindName("txtlo")).Text.ToString();
            }
        }
        private string LB
        {
            get
            {
                return ((TextBox)FindName("txtlb")).Text.ToString();
            }
        }
        private string ABTYPE
        {
            get
            {
                return ((TextBox)FindName("txtabtype")).Text.ToString();
            }
        }*/
        FLTICCGLLGData m_Data, m_DataX;
        public DataSet setResult;
        private DataRow dwUI;
        FLTICCGLLGReport m_rpt;
        bool flagstartmtr;
        ExcelX.ExcelLib.ExcelAgent m_exAgent;
        DataSet setANINFOR = new DataSet();
        DataTable tableUI;
        public DataSet setUI = new DataSet();
        string LASTINVDATE;
        public FLTICCGLLG()
        {
            InitializeComponent();
            this.Name = "历史库存查询"; 
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                base.SetUp(dicParamater);
                this.dicParamater = dicParamater;
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
                m_Data = new FLTICCGLLGData(OracleDB, proxy);
                InitDataBinding();
                tableUI = new DataTable("UIData");
                tableUI.Columns.Add("INVDATE", typeof(string));
                tableUI.Columns.Add("STARTDATE", typeof(string));
                tableUI.Columns.Add("LO", typeof(string));
                tableUI.Columns.Add("LB", typeof(string));
                txtBMONTH.Text = m_Data.Right("00" + DateTime.Now.Month.ToString(), 2);
                txtBYEAR.Text = DateTime.Now.Year.ToString();
                txtBDAY.Text = DateTime.Now.Day.ToString();
                flagstartmtr = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                setUI.Clear();
                setUI.AcceptChanges();
                finishedcount = 0;
                string bYear = txtBYEAR.Text.ToString();
                string bMonth = m_Data.Right("00" + txtBMONTH.Text.ToString(), 2);
                string bDay = m_Data.Right("00" + txtBDAY.Text.ToString(), 2);
                string WH = txtWH.Text.ToString().Trim();
                string LB = txtLB.Text.ToString().Trim();
                string startdate = bYear
                    + m_Data.Right("00" + bMonth, 2)
                    + m_Data.Right("00" + bDay.ToString().Trim(), 2);
                tableUI.Clear();
                dwUI = tableUI.NewRow();
                dwUI["STARTDATE"] = startdate;
                dwUI["LO"] = WH;
                dwUI["LB"] = LB;
                tableUI.Rows.Add(dwUI);
                m_Data.flagstartmtr = flagstartmtr;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    setUI = m_Data.GetData(dwUI);
                    //src.Merge(setResult);
                    MasterDg.DataContext = setUI.Tables["ICCGLLG"];
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void InitDataBinding()
        {
            this.setUI.Tables.Add("ICCGLLG");
            orlReadCommand = OracleDB.CreateCommand();

            #region "ABPOS"
            orlReadCommand.CommandText = "SELECT A.ICCGLDAT,A.ICCGLPARTIE,A.ICCGLLBEHI,A.ICCGLLO,A.ICCGLLB,A.ICCGLLP, "
                    + "  B.GPGART,B.GPGVAR,C.GSGARNBEZ,C.GSAGARNNR,B.GPGFARBE,D.GSCBEZ, "
                    + "  A.ICCGLBSTKG/1000 AS KG,A.ICCGLBSTNET/1000 AS NET, "
                    + "  (A.ICCGLBSTNET/1000+A.ICCGLTARA/100+A.ICCGLHUELGEW/100) AS GROSS "
                    + "  FROM ICC_GLLG A,GPDAT B,GSDAT C,GSCOLOR D "
                    + "  WHERE A.ICCGLMANDANT = B.GPMANDANT "
                    + " AND A.ICCGLPARTIE = B.GPPARTIE AND A.ICCGLPARTIEKZ = B.GPPARTIEKZ "
                    + " AND B.GPMANDANT = C.GSMANDANT(+) AND B.GPGART = C.GSART(+) "
                    + " AND B.GPGVAR = C.GSVAR(+) AND B.GPMANDANT = D.GSCMANDANT(+) "
                    + "  AND B.GPGART = D.GSCART(+) AND B.GPGVAR = D.GSCVAR(+) "
                    + "  AND B.GPGFARBE = D.GSCFARBE(+) AND ROWNUM<1 ";

            setResult = m_Data.Connect2Proxy(orlReadCommand);

            if (setResult.Tables.Count > 0)
            {
                setResult.Tables[0].TableName = "ICCGLLG";
                foreach (DataTable dtTemp in setResult.Tables)
                {
                    setUI = m_Data.ImportDataTable(setUI, dtTemp);
                }
            }
            #endregion

        }
        public void GenerateReport(string PrintType)
        {
            m_DataX = new FLTICCGLLGData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            m_rpt = new FLTICCGLLGReport(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = PrintType;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("EXCEL", dwUI["STARTDATE"].ToString());
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "JustExcel";
            GenerateReport(PrintType);
        }
        
    }
}
